home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / groutes.c < prev    next >
C/C++ Source or Header  |  1996-07-19  |  6KB  |  238 lines

  1. /* #Specification: routes / gateways identified manually
  2.     The user is allowed to program some routes to other network
  3.     using gateways. A default gateways may be identified (default_router)
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "netconf.h"
  9. #include "../misc/misc.h"
  10. #include "../xconf/xconf.h"
  11. #include "netconf.m"
  12.  
  13. NETCONF_HELP_FILE help_routes ("routes");
  14. static NETCONF_HELP_FILE help_defroute ("default_route");
  15.  
  16. CONFIG_FILE f_conf_routes (ETC_CONF_ROUTES
  17.     ,help_routes,CONFIGF_MANAGED|CONFIGF_OPTIONNAL);
  18.  
  19. struct GROUTES_INFO {
  20.     char defroute[MAX_LEN+1];
  21.     ROUTES routes;
  22. };
  23.  
  24. /*
  25.     Sort routes by destination
  26. */
  27. static int cmp_route_by_dest (const void *p1, const void *p2)
  28. {
  29.     ROUTE *h1 = *(ROUTE**)p1;
  30.     ROUTE *h2 = *(ROUTE**)p2;
  31.     return strcmp(h1->getdst(),h2->getdst());
  32. }
  33. /*
  34.     Write /etc/conf.routes
  35. */
  36. static void groutes_save(GROUTES_INFO &info)
  37. {
  38.     FILE *fout = f_conf_routes.fopen ("w");
  39.     if (fout != NULL){
  40.         if (info.defroute[0] != '\0'){
  41.             fprintf (fout,"-net default gw %s\n",info.defroute);
  42.         }
  43.         info.routes.write(fout);
  44.         fclose (fout);
  45.     }
  46. }
  47.  
  48.  
  49. static void groutes_editothers(GROUTES_INFO &info, int edit_host_route)
  50. {
  51.     int choice=0;
  52.     while (1){
  53.         ROUTE *tb[500];
  54.         int nbrt=0;
  55.         {
  56.             // Collect the entries which are host route or network route
  57.             // depending on edit_host_route
  58.             int nb_total = info.routes.getnb();
  59.             for (int i=0; i<nb_total; i++){
  60.                 ROUTE *pt = info.routes.getitem(i);
  61.                 int is_host = (pt->dst_is_host() != 0)
  62.                     + (edit_host_route != 0);
  63.                 if (is_host == 0 || is_host == 2){
  64.                     tb[nbrt++] = pt;
  65.                 }
  66.             }
  67.         }
  68.         qsort (tb,nbrt,sizeof(ROUTE *),cmp_route_by_dest);
  69.         char *menuopt[1000];
  70.         {
  71.             int j=0;
  72.             for (int i=0; i<nbrt; i++){
  73.                 menuopt[j++] = " ";
  74.                 menuopt[j++] = strdup(tb[i]->getdst());
  75.             }
  76.             menuopt[j] = NULL;
  77.         }
  78.         MENU_STATUS code = xconf_menu (
  79.             edit_host_route
  80.                 ? MSG_U(T_ROUTEHOSTS,"Route to other hosts")
  81.                 : MSG_U(T_ROUTENET,"Route to other networks")
  82.             ,MSG_U(I_ROUTEHOSTS
  83.              ,"If your network has access to other networks\n"
  84.               "you must tell how to reach those networks\n")
  85.             ,help_routes
  86.             ,NULL
  87.             ,NULL
  88.             ,NULL
  89.             ,MSG_U(I_TOADDROUTE,"to add a new route")
  90.             ,(const char**)menuopt,choice);
  91.         for (int k=0; k<nbrt; k++) free (menuopt[k*2+1]);
  92.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  93.             break;
  94.         }else if (code == MENU_ADD){
  95.             ROUTE *route = new ROUTE ("","",""
  96.                 ,edit_host_route ? "UGH" : "UG","");
  97.             if (route->edit(edit_host_route)==0){
  98.                 info.routes.add (route);
  99.                 groutes_save (info);
  100.             }else{
  101.                 delete route;
  102.             }
  103.         }else if (nbrt > 0){
  104.             if (code == MENU_OK){
  105.                 ROUTE *route = tb[choice];
  106.                 int ok = route->edit(edit_host_route);
  107.                 if (ok != -1){
  108.                     if (ok == 1){
  109.                         info.routes.remove_del (route);
  110.                     }
  111.                     groutes_save(info);
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117.  
  118. /*
  119.     Check if there is at least one route other than the default
  120.     Return != 0 if yes.
  121. */
  122. static int groutes_isothers(ROUTES &routes, int host_route)
  123. {
  124.     int ret = 0;
  125.     host_route = host_route != 0;
  126.     int nb = routes.getnb();
  127.     for (int i=0; i<nb; i++){
  128.         ROUTE *pt = routes.getitem(i);
  129.         if (!pt->is_default()){
  130.             int is_host_route = pt->dst_is_host() != 0;
  131.             if (host_route == is_host_route){
  132.                 ret = 1;
  133.                 break;
  134.             }
  135.         }
  136.     }
  137.     return ret;
  138. }
  139.  
  140. static int groutes_edit(
  141.     GROUTES_INFO &info)
  142. {
  143.     int ret = 0;
  144.     int choice=0;
  145.     while (1){
  146.         static const char *default_route = MSG_U(M_DEFROUTE,"the default route");
  147.         static const char *other_route_net = MSG_U(M_ROUTENET,"other routes to networks");
  148.         static const char *other_route_host = MSG_U(M_ROUTEHOSTS,"other routes to hosts");
  149.         static const char *routed = MSG_U(M_ROUTED,"the routed daemon");
  150.         char default_route_str[80];
  151.         char other_route_str_net[80];
  152.         char other_route_str_host[80];
  153.         static const char *menuopt[]={
  154.             MSG_U(M_SET,"Set"),        NULL,
  155.             " ",                    NULL,
  156.             " ",                    NULL,
  157.             MSG_U(M_CONFIG,"Configure"),routed,
  158.             NULL
  159.         };
  160.         menuopt[1] = menu_setupopt(default_route_str,default_route
  161.             ,info.defroute);
  162.         menuopt[3] = menu_setupopt(other_route_str_net,other_route_net
  163.             ,groutes_isothers(info.routes,0)
  164.             ? MSG_U(M_CONFIGURED,"configured") : MSG_U(M_NONE,"none"));
  165.         menuopt[5] = menu_setupopt(other_route_str_host,other_route_host
  166.             ,groutes_isothers(info.routes,1)
  167.             ? MSG_R(M_CONFIGURED) : MSG_R(M_NONE));
  168.         MENU_STATUS code = xconf_menu (
  169.             MSG_U(T_ROUTES,"Routes to other network")
  170.             ,MSG_U(I_ROUTES
  171.              ,"If your network has access to other networks\n"
  172.               "you must tell how to reach those networks\n"
  173.               "For many setup, simply setting the default route\n"
  174.               "is good enough\n"
  175.               "\n"
  176.               "If you only talk to machine on the local network\n"
  177.               "then all this is not necessary.\n")
  178.             ,help_routes
  179.             ,NULL
  180.             ,NULL
  181.             ,NULL
  182.             ,NULL
  183.             ,menuopt,choice);
  184.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  185.             ret = -1;
  186.             break;
  187.         }else{
  188.             const char *key = menuopt[choice*2+1];
  189.             if (key == default_route_str){
  190.                 DIALOG dia;
  191.                 dia.newf_str("",info.defroute
  192.                     ,sizeof(info.defroute)-1);
  193.                 if (dia.edit (MSG_U(T_DEFGTW,"Default gateway")
  194.                     ,MSG_U(I_DEFGTW
  195.                      ,"Enter the IP number of the main gateway\n")
  196.                     ,help_defroute.getpath()
  197.                     ,0) == MENU_ACCEPT){
  198.                     groutes_save(info);
  199.                 }
  200.             }else if (key == other_route_str_net){
  201.                 groutes_editothers (info,0);
  202.             }else if (key == other_route_str_host){
  203.                 groutes_editothers (info,1);
  204.             }else if (key == routed){
  205.                 routed_edit();
  206.             }
  207.         }
  208.     }
  209.     return ret;
  210. }
  211.  
  212.  
  213. /*
  214.     Edite /etc/conf.routes
  215. */
  216. void netconf_editroutes()
  217. {
  218.     GROUTES_INFO info;
  219.     info.defroute[0] = '\0';
  220.     FILE *fin = f_conf_routes.fopen ("r");
  221.     if (fin != NULL){
  222.         char buf[300];
  223.         int noline;
  224.         while (fgets_strip (buf,sizeof(buf)-1,fin,&noline)!=NULL){
  225.             ROUTE *pt = new ROUTE (buf,noline);
  226.             if (pt->is_default()){
  227.                 strcpy (info.defroute,pt->getgateway());
  228.                 delete pt;
  229.             }else{
  230.                 info.routes.add (pt);
  231.             }
  232.         }
  233.         fclose (fin);
  234.     }
  235.     groutes_edit(info);
  236. }
  237.  
  238.